package com.good.webadmin.Controller;


import cn.hutool.crypto.SignUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.good.domain.controller.BaseController;
import com.good.domain.entity.AdminRole;
import com.good.domain.entity.AdminRoleModule;
import com.good.domain.models.PostID;
import com.good.domain.models.Result;
import com.good.domain.service.IAdminLogService;
import com.good.domain.service.IAdminRoleModuleService;
import com.good.domain.service.IAdminRoleService;
import com.good.domain.service.IUserLoginToken;
import com.good.webadmin.Models.Params.AddRole;
import com.good.webadmin.Models.Params.EditRole;
import com.good.webadmin.Models.TokenUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqz
 * @since 2022-03-05
 */
@RestController
@RequestMapping("/webadmin/admin-role")
@Api(tags = "管理员角色")
public class AdminRoleController extends BaseController {

    @Autowired
    private IAdminRoleService iAdminRoleService;
    @Autowired
    private IAdminRoleModuleService iAdminRoleModuleService;
    @Autowired
    private IAdminLogService adminLogService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加角色，keys=rolename")
    public Result Add(TokenUser tokenUser, @RequestBody AddRole addRole){
//        if(!signUtil.check(addRole.timestamp,addRole.sign,addRole.getRoleName() )) {
             QueryWrapper<AdminRole> queryWrapper=new QueryWrapper<>();
              queryWrapper.eq("role_name", addRole.getRoleName());
            AdminRole adminRole = iAdminRoleService.getOne(queryWrapper);
            if (adminRole != null) {
                return  OtherError("角色名字不能重复");
            } else {
                adminRole = new AdminRole();
                adminRole.setRoleName(addRole.getRoleName());
                adminRole.setDescription(addRole.getDescription());
                adminRole.setUid(tokenUser.getUserId());
                iAdminRoleService.save(adminRole);
                //日志输出
                adminLogService.setAdminLog(tokenUser.getUserId(), "[添加角色成功]", addRole);
                return Success("添加角色成功");
            }
//        }else
//        {
//            return Result.SignError();
//        }
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation("修改角色")
    public Result Edit(TokenUser tokenUser, @RequestBody EditRole editRole){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_name", editRole.getRoleName());
        AdminRole adminRole = iAdminRoleService.getOne(queryWrapper);
        if (adminRole != null) {
            return OtherError("角色名字不能重复");
        } else {
            adminRole =iAdminRoleService.getById(editRole.getId());
            adminRole.setRoleName(editRole.getRoleName());
            adminRole.setDescription(editRole.getDescription());
            adminRole.setUid(tokenUser.getUserId());
            adminRole.setUid(tokenUser.getUserId());
            adminRole.setUpdateTime(null);
            var ret= iAdminRoleService.updateById(adminRole);
            if(ret) {
                //日志输出
                adminLogService.setAdminLog(tokenUser.getUserId(), "[修改角色成功]", editRole);
                return Success("修改角色成功");
            }else {
                return  Error500("修改角色失败");
            }
        }
    }
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ApiOperation("删除角色")
    public Result Del(TokenUser tokenUser,@RequestBody PostID postID)
    {

//            AdminRole adminRole= iAdminRoleService.getById(postID.getId());
//            if(adminRole==null){
//                return Result.NoDataError();
//            }
            QueryWrapper<AdminRole> queryWrapperAdmin=new QueryWrapper<>();
            queryWrapperAdmin.eq("id",postID.getId());
            AdminRole adminRole=new AdminRole();
            adminRole.setDeleted(1);
            adminRole.setUpdateTime(null);
            adminRole.setUid(tokenUser.getUserId());


            var ret=iAdminRoleService.removeById(postID.getId());
            if(ret){
                QueryWrapper<AdminRoleModule> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("role_id",postID.getId());
                iAdminRoleModuleService.remove(queryWrapper);
                //日志输出
                adminLogService.setAdminLog(tokenUser.getUserId(), "[删除角色成功]", postID);
                return Success("删除角色成功");
            }
            else {
                return  Error500("删除角色失败");
            }

    }
    @PostMapping("List")
    @ApiOperation("角色列表")
    @IUserLoginToken
    public Result List(){
        QueryWrapper<AdminRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","uid","description","insert_time","role_name");
        var roles=iAdminRoleService.listMaps(queryWrapper);
        return Success(roles);
    }
}
